package com.jt.www.hadoopcould.controller;

import com.jt.www.hadoopcould.model.user;
import com.jt.www.hadoopcould.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@SpringBootApplication
public class userController {
    @Autowired
    HttpServletRequest request;
    @Autowired
    private UserService userService;


    @RequestMapping("/login")
    public String goLogin(){
        return "user/login";
    }
    @RequestMapping("/goUp")
    public String goUp()
    {
        return "filemain";
    }


    @RequestMapping("/registr")
    public String goRegistr(){
        return "user/registr";
    }



    @RequestMapping("/index")
    public String goIndex(Model model){
       user user=(user)request.getSession().getAttribute("user");
       if (null ==user){
           return "user/login";
       }else{
           model.addAttribute(user);
           return "index";
       }

    }

    @RequestMapping("/registrmgr")
    @ResponseBody
    public String registrMgr(user user){
        user testu=userService.findById(user.getUserid());

        if (null !=testu){
            return "error";
        }else{
            userService.save(user);
            return "sucess";
        }


    }
    @RequestMapping("/loginmgr")
    @ResponseBody
    public String loginMgr(user user){
        user testu=userService.findById(user.getUserid());
        if (null !=testu){
            if (user.getUserid().equals(testu.getUserid())&&user.getPassword().equals(testu.getPassword())){
                request.getSession().setAttribute("user",user);
                return "sucess";
            }else{
                return "error";
            }

        }else{

            return "error";

        }


    }




}
